package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.dto.AddDishDto;
import com.itheima.dto.UpdateDishDto;
import com.itheima.service.DishService;
import com.itheima.vo.DishVO;
import com.itheima.vo.DishVoPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author msi
 */
@RequestMapping("/dish")
@RestController
public class DishController {

    @Autowired
    private DishService service;

    @PostMapping("")
    public R<String> addDish(@RequestBody AddDishDto addDishDto) {
        Boolean flag = service.addDish(addDishDto);

        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R<DishVO> selectById(@PathVariable String id) {

        DishVO dishVO = service.selectById(id);
        if (dishVO == null) {
            return R.error("查不到");
        }
        return R.success(dishVO);
    }

    @GetMapping("/page")
    public R<DishVoPage> getPage(Integer page, Integer pageSize) {

        DishVoPage dishVoPage = service.getPage(page, pageSize);

        return R.success(dishVoPage);
    }

    @DeleteMapping("")
    public R<String> delete(@RequestParam("ids") String ids) {

        Boolean flag = service.deleteById(ids);

        if (flag) {
            return R.success("删除成功");
        }
        return null;
    }

    @PutMapping("")
    public R<String> update(@RequestBody UpdateDishDto updateDishDto) {
        Boolean flag = service.update(updateDishDto);
        if (flag) {
            return R.success("修改成功");
        }
        return null;
    }

    @PostMapping("/status/{status}")
    public R<String> changeStatus
            (@PathVariable Integer status, @RequestParam("ids") String ids) {
        Boolean flag = service.changeStatus(status, ids);
        if (flag) {
            return R.success("操作成功");
        }
        return null;
    }

    @GetMapping("/list")
    public R<List<DishVO>> getList(@RequestParam("categoryId") String categoryId) {
        List<DishVO> dishList = service.getList(categoryId);

        return R.success(dishList);
    }


}
